package com.pb.metadata.controller;

import com.pb.metadata.service.MateCodeService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 码表管理模块
 * @author liurongjun
 * @date 2021/02/22
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/mateCodeController")
public class MateCodeController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private MateCodeService mateCodeService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "PARAM_CODE_TABLE";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "码表管理";

    /**
     * 查询分页数据
     *
     * @param map     查询参数
     * @param page    页码
     * @return Result 返回数据
     */
    @GetMapping("selectByPage")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectByPage(@RequestParam Map<String, Object> map, Page page) {
        return mateCodeService.selectByPage(map, page);
    }

    /**
     * 保存码表相关信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        return mateCodeService.insert(paramMap, request);
    }

    /**
     * 修改码表相关信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap, HttpServletRequest request){
        Result update = mateCodeService.update(paramMap, request);
        return update;
    }

    /**
     * 查询数据库用户
     *
     * @return Result 返回数据
     */
    @GetMapping("getBaseUserList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getBaseUserList() {
        return mateCodeService.getBaseUserList();
    }

    /**
     * 删除码表相关信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        mateCodeService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 启用停用码表
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("startOrStop")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result startOrStop(@RequestBody  List<Map<String,Object>> paramMap){
        mateCodeService.startOrStop(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 锁定码表相关信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("lockedBatch")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result lockedDate(@RequestBody List<String> paramMap) {
        mateCodeService.lockedDate(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 提交码表相关信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("freeBatch")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result freeDate(@RequestBody List<String> paramMap) {
        mateCodeService.freeDate(paramMap);
        return Result.OK_EMPTY;
    }

}
